/*
 * Copyright © 2022-now 尛飛俠（Denvie） All rights reserved.
 */

package cn.denvie.sentinel.persistent.pull.file.assist;

import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition;
import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule;
import com.alibaba.csp.sentinel.datasource.Converter;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule;
import com.alibaba.csp.sentinel.slots.system.SystemRule;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 规则列表解析工具类。
 *
 * @author Denvie
 * @date 2022/4/5
 * @since 1.0.0
 */
public class RuleListConverterUtils {

    public static final Converter<String, List<FlowRule>> flowRuleListParser = new Converter<>() {
        @Override
        public List<FlowRule> convert(String source) {
            return SentinelConverterWrapper.convert(FlowRule.class, source).stream()
                    .map(obj -> (FlowRule) obj)
                    .collect(Collectors.toList());
        }
    };

    public static final Converter<String, List<DegradeRule>> degradeRuleListParser = new Converter<>() {
        @Override
        public List<DegradeRule> convert(String source) {
            return SentinelConverterWrapper.convert(DegradeRule.class, source).stream()
                    .map(obj -> (DegradeRule) obj)
                    .collect(Collectors.toList());
        }
    };

    public static final Converter<String, List<SystemRule>> sysRuleListParser = new Converter<>() {
        @Override
        public List<SystemRule> convert(String source) {
            return SentinelConverterWrapper.convert(SystemRule.class, source).stream()
                    .map(obj -> (SystemRule) obj)
                    .collect(Collectors.toList());
        }
    };

    public static final Converter<String, List<ParamFlowRule>> paramFlowRuleListParser = new Converter<>() {
        @Override
        public List<ParamFlowRule> convert(String source) {
            return SentinelConverterWrapper.convert(ParamFlowRule.class, source).stream()
                    .map(obj -> (ParamFlowRule) obj)
                    .collect(Collectors.toList());
        }
    };

    public static final Converter<String, List<AuthorityRule>> authorityRuleParser = new Converter<>() {
        @Override
        public List<AuthorityRule> convert(String source) {
            return SentinelConverterWrapper.convert(AuthorityRule.class, source).stream()
                    .map(obj -> (AuthorityRule) obj)
                    .collect(Collectors.toList());
        }
    };

    public static final Converter<String, Set<GatewayFlowRule>> gatewayParser = new Converter<>() {
        @Override
        public Set<GatewayFlowRule> convert(String source) {
            return SentinelConverterWrapper.convert(GatewayFlowRule.class, source).stream()
                    .map(obj -> (GatewayFlowRule) obj)
                    .collect(Collectors.toSet());
        }
    };

    public static final Converter<String, Set<ApiDefinition>> apiDefinitionParser = new Converter<>() {
        @Override
        public Set<ApiDefinition> convert(String source) {
            return SentinelConverterWrapper.convert(ApiDefinition.class, source).stream()
                    .map(obj -> (ApiDefinition) obj)
                    .collect(Collectors.toSet());
        }
    };

    public static final Converter<List<FlowRule>, String> flowRuleEncoder = new Converter<>() {
        @Override
        public String convert(List<FlowRule> source) {
            return SentinelConverterWrapper.writeValueAsString(source);
        }
    };

    public static final Converter<List<SystemRule>, String> sysRuleEncoder = new Converter<>() {
        @Override
        public String convert(List<SystemRule> source) {
            return SentinelConverterWrapper.writeValueAsString(source);
        }
    };

    public static final Converter<List<DegradeRule>, String> degradeRuleEncoder = new Converter<>() {
        @Override
        public String convert(List<DegradeRule> source) {
            return SentinelConverterWrapper.writeValueAsString(source);
        }
    };

    public static final Converter<List<ParamFlowRule>, String> paramRuleEncoder = new Converter<>() {
        @Override
        public String convert(List<ParamFlowRule> source) {
            return SentinelConverterWrapper.writeValueAsString(source);
        }
    };

    public static final Converter<List<AuthorityRule>, String> authorityEncoder = new Converter<>() {
        @Override
        public String convert(List<AuthorityRule> source) {
            return SentinelConverterWrapper.writeValueAsString(source);
        }
    };

    public static final Converter<Set<GatewayFlowRule>, String> gatewayEncoder = new Converter<>() {
        @Override
        public String convert(Set<GatewayFlowRule> source) {
            return SentinelConverterWrapper.writeValueAsString(source);
        }
    };

    public static final Converter<Set<ApiDefinition>, String> apiDefinitionEncoder = new Converter<>() {
        @Override
        public String convert(Set<ApiDefinition> source) {
            return SentinelConverterWrapper.writeValueAsString(source);
        }
    };
}
